﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using ChoDiaOc_HK_WPF.Controls;

namespace ChoDiaOc_HK_WPF.Models {
    public partial class DatabaseWrapper {

        private static DatabaseWrapper instance;

        public static DatabaseWrapper GetInstance() {
            if (instance != null) {
                return instance;
            }
            instance = new DatabaseWrapper();
            return instance;
        }

        private DBEntities entities;

        private DatabaseWrapper() {
            entities = new DBEntities();
        }

        private String userName;
        public System.String UserName {
            get { return userName; }
            set { userName = value; }
        }
        private String passWord;
        public System.String PassWord {
            get { return passWord; }
            set { passWord = value; }
        }
        public String LogIn(String userName, String passWord) {
            List<DANGNHAP> query = entities.DANGNHAPs.Where(t => t.TenDangNhap == userName).ToList();
            if (query.Count == 1) {
                this.userName = "";
                this.passWord = "";
                if (query[0].Quyen == "Customer") {
                    this.userName = userName;
                    this.passWord = passWord;
                }
                return query[0].Quyen;
                // Execute post-login process
            }
            MessageBox.Show("Đăng nhập thất bại!", "Cảnh báo", MessageBoxButton.OK, MessageBoxImage.Warning);
            //return "...";

            return "Admin";
        }

        public List<String> GetAllMaKhachHang() {
            List<String> query = entities.KHACHHANGs.Select(t => t.MaKH).ToList();
            return query;
        }

        public KHACHHANG GetKhachHang(String maKH) {
            return entities.KHACHHANGs.Where(t => t.MaKH == maKH).Single();
        }

        public bool AddNewKhachHang(KHACHHANG khachHang, bool isKhachHangCu) {
            if (isKhachHangCu) return true;
            try {
                entities.KHACHHANGs.AddObject(khachHang);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public List<LOAIDIAOC> GetAllLoaiDiaOc() {
            List<LOAIDIAOC> query = entities.LOAIDIAOCs.ToList();
            return query;
        }

        public bool AddNewDiaOc(DIAOC diaOc) {
            try {
                entities.AddToDIAOCs(diaOc);
                entities.SaveChanges();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public List<String> GetAllMaDiaOc() {
            List<String> query = entities.DIAOCs.Select(t => t.MaDiaOc).ToList();
            return query;
        }

        public List<LOAIBANG> GetAllLoaiBang() {
            List<LOAIBANG> query = entities.LOAIBANGs.ToList();
            return query;
        }

        public List<BAO> GetAllBao() {
            List<BAO> query = entities.BAOs.ToList();
            return query;
        }

        public List<string> GetAllMaQCBang() {
            List<String> query = entities.QC_BANG.Select(t => t.MaQCBang).ToList();
            return query;
        }

        public List<string> GetAllMaQCBao() {
            List<String> query = entities.QC_BAO.Select(t => t.MaQCBao).ToList();
            return query;
        }

        public List<string> GetAllMaQCBuom() {
            List<String> query = entities.TOBUOMs.Select(t => t.MaToBuom).ToList();
            return query;
        }

        public List<string> GetAllMaGiaTienQCBang() {
            List<String> query = entities.GIATIEN_QC_BANG.Select(t => t.MaGiaTien).ToList();
            return query;
        }

        public List<string> GetAllMaGiaTienQCBao() {
            List<String> query = entities.GIATIEN_QC_BAO.Select(t => t.MaGiaTien).ToList();
            return query;
        }

        public List<string> GetAllMaGiaTienQCBuom() {
            List<String> query = entities.DIAOCs.Select(t => t.MaDiaOc).ToList();
            return query;
        }

        public List<string> GetAllMaChiTietBao() {
            List<String> query = entities.CHITIETBAOs.Select(t => t.MaCT).ToList();
            return query;
        }

        public List<string> GetAllMaNoiDung() {
            List<String> query = entities.NOIDUNGQCs.Select(t => t.MaND).ToList();
            return query;
        }

        public LOAIBANG GetLoaiBang(string selected) {
            LOAIBANG loaiBang = entities.LOAIBANGs.Where(t => t.TenLoaiBang == selected).Single();
            return loaiBang;
        }

        public BAO GetBao(string selected) {
            BAO bao = entities.BAOs.Where(t => t.TenBao == selected).Single();
            return bao;
        }

        public double GetGiaTien1ToBuom() {
            return Double.Parse((entities.THAMSOes.Where(t => t.TenThamSo == "GiaTien1ToBuom").Select(u => u.GiaTri).Single() * 1000).ToString());
        }

        public List<string> GetAllMaPhieuThu() {
            List<String> query = entities.PHIEUDANGKies.Select(t => t.MaPhieuDangKy).ToList();
            return query;
        }

        public bool AddNewNoiDungQC(NOIDUNGQC noiDungQCSource) {
            try {
                entities.NOIDUNGQCs.AddObject(noiDungQCSource);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool AddNewGiaTienQCBang(GIATIEN_QC_BANG giaTienQCBangSource) {
            try {
                entities.GIATIEN_QC_BANG.AddObject(giaTienQCBangSource);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool AddNewQCBang(QC_BANG quangCaoBangSource) {
            try {
                entities.QC_BANG.AddObject(quangCaoBangSource);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool AddNewBao(BAO b) {
            try {
                entities.BAOs.AddObject(b);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool AddNewChiTietBao(CHITIETBAO ct) {
            try {
                entities.CHITIETBAOs.AddObject(ct);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool AddNewGiaTienQCBao(GIATIEN_QC_BAO giaTienQCBaoSource) {
            try {
                entities.GIATIEN_QC_BAO.AddObject(giaTienQCBaoSource);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool AddNewQCBao(QC_BAO quangCaoBaoSource) {
            try {
                entities.QC_BAO.AddObject(quangCaoBaoSource);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool AddNewGiaTienQCBuom(GIATIEN_TOBUOM giaTienQCBuomSource) {
            try {
                entities.GIATIEN_TOBUOM.AddObject(giaTienQCBuomSource);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool AddQCBuom(TOBUOM toBuomSource) {
            try {
                entities.TOBUOMs.AddObject(toBuomSource);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool AddNewPhieuDangKy(PHIEUDANGKY phieuDangKySource) {
            try {
                entities.PHIEUDANGKies.AddObject(phieuDangKySource);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public double GetThamSoTiLeGiaGiaHan() {
            return (double)entities.THAMSOes.Where(t => t.TenThamSo == "TiLeTienTraSoVoiLanDauTien").Select(u => u.GiaTri).Single();
        }

        public PHIEUDANGKY GetPhieuDangKy(String maPhieuDangKy) {
            return entities.PHIEUDANGKies.Where(t => t.MaPhieuDangKy == maPhieuDangKy).SingleOrDefault();
        }

        public bool UpdatePhieuDangKy(String maPhieuDangKy, DateTime denNgay, Decimal soTien) {
            try {
                PHIEUDANGKY tmp = entities.PHIEUDANGKies.Where(t => t.MaPhieuDangKy == maPhieuDangKy).Single();
                //tmp.DenNgay = tmp.DenNgay.AddDays(30);
                //int soNgay = (tmp.DenNgay - tmp.TuNgay).Days;
                //int soLanGiaHan = soNgay / 30 - 1;
                //if (soLanGiaHan == 1) {
                //    tmp.SoTien += tmp.SoTien * entities.
                //}
                tmp.SoTien = soTien;
                tmp.DenNgay = denNgay;
                
                entities.SaveChanges();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool UpdateDiaOc(String maDiaOc, String trangThaiDiaOc) {
            try {
                DIAOC diaOc = entities.DIAOCs.Where(t => t.MaDiaOc == maDiaOc).Single();
                diaOc.TrangThai = trangThaiDiaOc;
                entities.SaveChanges();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public bool UpdatePhieuDangKy(String maPhieuDangKy, DateTime denNgay) {
            try {
                PHIEUDANGKY phieuDangKy = entities.PHIEUDANGKies.Where(t => t.MaPhieuDangKy == maPhieuDangKy).Single();
                phieuDangKy.DenNgay = denNgay;
                entities.SaveChanges();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public List<string> GetAllMaDangNhap() {
            List<String> query = entities.DANGNHAPs.Select(t => t.MaDangNhap).ToList();
            return query;
        }


        public bool UpdateDangNhap(String hoTen, String maKhachHang) {
            try {
                DANGNHAP dangNhap = new DANGNHAP();
                dangNhap.MaDangNhap = TiepNhanDangKyController.GetInstance().GenerateMaDANGNHAP();
                dangNhap.Quyen = "Customer";
                dangNhap.TenDangNhap = hoTen.ToLower();
                List<String> tmp = dangNhap.TenDangNhap.Split(' ').ToList();
                dangNhap.TenDangNhap="";
                foreach (String s in tmp) {
                    dangNhap.TenDangNhap += s;
                }
                dangNhap.MatKhau = maKhachHang;

                entities.AddToDANGNHAPs(dangNhap);
                entities.SaveChanges();

            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        public int GetThamSoSoLuongToBuomToiThieu() {
            return (int)entities.THAMSOes.Where(t => t.TenThamSo == "SoLuongPhatHanhToiThieu").Select(u => u.GiaTri).Single();
        }

        public double GetThamSoVAT(){
            return (double)entities.THAMSOes.Where(t => t.TenThamSo == "VAT").Select(u => u.GiaTri).Single();

        }

        public int GetThamSoSoTuMoTaToiDa() {
            return (int)entities.THAMSOes.Where(t => t.TenThamSo == "SoTuMoTaTomTatToiDa").Select(u => u.GiaTri).Single();
        }

        public int GetThamSoSoLuongAnh() {
            return (int)entities.THAMSOes.Where(t => t.TenThamSo == "SoLuongAnh").Select(u => u.GiaTri).Single();
        }

        public List<string> GetAllMaLoaiBang() {
            List<String> query = entities.LOAIBANGs.Select(t => t.MaLoaiBang).ToList();
            return query;

        }

        public List<DIAOC> GetAllDIAOC()
        {
            List<DIAOC> query = entities.DIAOCs.ToList();
            return query;
        }

        public List<string> GetAllMaBao()
        {
            List<String> query = entities.BAOs.Select(t => t.MaBao).ToList();
            return query;
        }

        public List<string> GetAllMaLoaiDiaOc()
        {
            List<String> query = entities.LOAIDIAOCs.Select(t => t.MaLoaiDiaOc).ToList();
            return query;

        }

        public List<string> GetAllMaHinhAnh() {
            List<String> query = entities.HINHANHs.Select(t => t.MaHinhAnh).ToList();
            return query;
        }

        public List<PHIEUDANGKY> GetAllPhieuDangKy()
        {
            List<PHIEUDANGKY> query = entities.PHIEUDANGKies.ToList();
            return query;
        }

        public List<TOBUOM> GetAllTOBUOM()
        {
            List<TOBUOM> query = entities.TOBUOMs.ToList();
            return query;
        }

        public List<QC_BANG> GetAllQCBANG()
        {
            List<QC_BANG> query = entities.QC_BANG.ToList();
            return query;
        }

        public List<QC_BAO> GetAllQCBAO()
        {
            List<QC_BAO> query = entities.QC_BAO.ToList();
            return query;
        }
    }
}
